/**
 * 客户端到服务器连接池
 */
package com.tport.client.service.impl;

import java.io.InputStream;
import java.net.Socket;
import java.net.SocketAddress;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import com.tport.Param;

public class PoolClientFactory implements PooledObjectFactory<Socket>  {

    @Override
    public void activateObject(PooledObject<Socket> arg0) throws Exception {
        Socket socket = arg0.getObject();
        // SocketAddress sa = new SocketAddress();
        
        // socket.connect("192.168.0.111","");
        
    }

    @Override
    public void destroyObject(PooledObject<Socket> arg0) throws Exception {
        Socket socket = arg0.getObject();
        socket.close();        
        
    }

    @Override
    public PooledObject<Socket> makeObject() throws Exception {
    	Socket socket  = new Socket(Param.getServerIp(), Param.getServerPort());
        PooledObject<Socket> p = new DefaultPooledObject<Socket>(socket);
        InputStream is = socket.getInputStream();
		PortClientReadThread pthread = new PortClientReadThread();
		pthread.setIs(is);
		pthread.setSsocket(socket);
		pthread.run();
        return p;
    }

    @Override
    public void passivateObject(PooledObject<Socket> arg0) throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean validateObject(PooledObject<Socket> arg0) {
        // TODO Auto-generated method stub
        return false;
    }
    
}
